{include file="public/header" /}
<body class="gray-bg">
<div class="wrapper wrapper-content">
    <div class="ibox-title">
        <h5>编辑管理员</h5>
    </div>
    <div class="ibox-content layui-form">
        <div class="layui-row layui-col-space10 layui-form-item ">
            <input type="hidden" name="id" value="{$user.id}">
            <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                <label class="layui-form-label">名称：</label>
                <div class="layui-input-block">
                    <input type="text" name="username" lay-verify="required|username" placeholder="管理员名称" class="layui-input " value="{$user.username}">
                </div>
            </div>
            <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                <label class="layui-form-label">角色：</label>
                <div class="layui-input-block">
                    <select name="groupid" lay-verify="required" lay-search="">
                        <option value="">请选择角色</option>
                        {if !empty($role)}
                        {foreach name="role" item="vo"}
                        <option value="{$vo.id}" {eq name="$user.groupid" value="$vo.id"}selected{/eq}>{$vo.title}</option>
                        {/foreach}
                        {/if}
                    </select>
                </div>
            </div>
            <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                <label class="layui-form-label">头像：</label>
                <div class="layui-input-block">
                    <input type="hidden" id="data_photo" name="portrait" lay-verify="headCrop" value="{$user.portrait}">
                    <div id="fileList" class="uploader-list" style="float:right"></div>
                    <div  style="float:left" class="layui-btn" onclick="wk.layer_show1('上传头像',$('#headCrop'),700,570);">选择头像</div>
                    <img id="img_data" class="img-circle" height="80px" width="80px" style="float:left;margin-left: 50px;margin-top: -10px;cursor:pointer;"  src="{$user.portrait}" onerror="this.src='/static/admin/images/head_default.gif'"/>
                </div>
            </div>
            <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                <label class="layui-form-label">密码：</label>
                <div class="layui-input-block">
                    <input type="password" name="password" lay-verify="pass" placeholder="不修改请留空" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                <label class="layui-form-label">真实姓名：</label>
                <div class="layui-input-block">
                    <input type="text" name="real_name" lay-verify="required|realname" placeholder="管理员真实姓名" class="layui-input" value="{$user.real_name}">
                </div>
            </div>
            <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                <label class="layui-form-label">联系方式：</label>
                <div class="layui-input-block">
                    <input type="text" name="phone" placeholder="管理员联系方式" lay-verify="required|phone" class="layui-input " value="{$user.phone}">
                </div>
            </div>
            {eq name="$user.groupid" value="3"}
            <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                <label class="layui-form-label">积分池：</label>
                <div class="layui-input-block">
                    <input type="text" name="pool" lay-verify="required" placeholder="积分池数量" class="layui-input " oninput="value=value.replace(/[^\d]/g,'')"  value="{$user.pool}" id="pool">
                </div>
            </div>
            {/eq}
            <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                <label class="layui-form-label">可支配积分：</label>
                <div class="layui-input-block">
                    <input type="text" name="integral" lay-verify="required" placeholder="可支配积分数量" class="layui-input " oninput="value=value.replace(/[^\d]/g,'')"  value="{$user.integral}" id="inte" {eq name="$user.groupid" value="3"}disabled{/eq} >
                </div>
            </div>
            {if condition="$user.type == 3" }
            <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                <label class="layui-form-label">开始时间：</label>
                <div class="layui-input-block">
                    <input type="text"  name="sign_start" lay-verify="required|sign"  class="layui-input layer-date sign" placeholder="签到开始时间" value="{$user.sign_start}" />
                </div>
            </div>
            <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                <label class="layui-form-label">结束时间：</label>
                <div class="layui-input-block">
                    <input type="text"  name="sign_end" lay-verify="required|sign"  class="layui-input layer-date sign1" placeholder="签到结束时间" value="{$user.sign_end}" />
                </div>
            </div>
            {/if}
            <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit="" lay-filter="component-form-element">保存
                    </button>
                    <button class="layui-btn layui-btn-primary" onclick="history.go(-1)">返回</button>
                </div>
            </div>
        </div>
    </div>
</div>
{include file="public/footer" /}
<script type="text/javascript" src="__JS__/Crop.js"></script>
<script>
    var po = "{$user.pool}";
    var ini = "{$user.integral}";
    $('#pool').bind('input propertychange',function(){
        var ca = $(this).val();
        if(parseInt(ca) > parseInt(po)){
            var now = (parseInt(ca) -  parseInt(po)) + parseInt(ini);
            $("#inte").val(now);
        }else{
            if(parseInt(po)-parseInt(ca) > parseInt(ini)){
                $('#pool').val(po);
                $("#inte").val(ini);
            }else{
                $("#inte").val(parseInt(ini)-(parseInt(po)-parseInt(ca)));
            }
        }
    })
</script>
<script>
    $("#sureCut").on("click",function () {
        if ($("#tailoringImg").attr("src") == null ){
            return false;
        }else{
            var cas = $('#tailoringImg').cropper('getCroppedCanvas');
            var base64url = cas.toDataURL('image/png');
            $("#img_data").attr('src',base64url).show();
            $('#data_photo').val(base64url);
            layer.closeAll();
        }
    });
</script>
<script>
    layui.use(['form','laydate'], function() {
        var form = layui.form
            ,laydate = layui.laydate

        laydate.render({
            elem: '.sign'
            ,type: 'date'
            // ,show: true
            ,format:'yyyy-MM-dd'
            ,trigger: 'click'
        });

        laydate.render({
            elem: '.sign1'
            ,type: 'date'
            // ,show: true
            ,format:'yyyy-MM-dd'
            ,trigger: 'click'
        });

        form.verify({
            username:function(value,item){
                if(!/^[\S]{2,10}$/.test(value)){
                    return '名称必须2到10字符，且不能出现空格';
                }
                var checkResult = "";
                $.ajax({
                    url:"{:url('User/checkName')}",
                    type:'post',
                    data:"username="+value +'&id='+ $('input[name=id]').val(),
                    async: false,
                    success:function(res){
                        if(res.code == 100){
                            checkResult = "该名称已存在";
                        }
                    }
                })
                return checkResult;
            },
            sign:function (value,item) {
                var _start = $("input[name=sign_start]").val()
                    ,_end = $("input[name=sign_end]").val()
                if(_end < _start){
                    return "结束时间不可小于开始时间"
                }
            }
            ,pass:function(value,item){
                if(value != "" && !/^[\S]{6,12}$/.test(value)){
                    return '密码必须6到12位，且不能出现空格';
                }
            }
            ,realname:[/^[\S]{2,10}$/,'真实姓名必须2到10字符，且不能出现空格']

        });
        form.on('submit(component-form-element)', function (data) {
            $('.layui-btn').addClass('layui-disabled').attr('disabled','disabled');
            $.ajax({
                url:"{:url('userEdit')}",
                type:'post',
                dataType:'json',
                data:data.field,
                success:function(res){
                    if (res.code == 200) {
                        if(res.data == 100){
                            wk.success(res.msg,"{:url('admin/Login/loginOut')}");
                        }else{
                            if (res.type == "have") {
                                wk.success(res.msg,'','top.location.reload();');
                            } else {
                                wk.success(res.msg,"{:url('User/index')}")
                            }
                        }
                    } else {
                        wk.error(res.msg,'','$(".layui-btn").removeClass(\'layui-disabled\').removeAttr(\'disabled\')');
                    }
                }
            })
        });
    });
</script>
</body>
</html>